package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author ：cui
 * @date ：Created in 2019/7/1
 * @description ：
 * @version: 1.0
 */
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }
    //查询两月之间的会员报告
    @Override
    public Map<String, Object> getMemberReport(String[] monthList) {
        /*ArrayList<String> months = new ArrayList<>();
        //获取最近12个月
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            months.add(calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1));
            calendar.add(Calendar.MONTH,+1);
        }*/

        List<String> months = null;
        try {
            months = DateUtils.getMonthBetween(monthList[0], monthList[1], "yyyy-MM");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            int count = memberDao.getMemberCountByMonth(month+"-31");
            memberCount.add(count);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return map;
    }
    //查询近一年的会员报告

    @Override
    public Map<String, Object> getMemberReport4year() {
        ArrayList<String> months = new ArrayList<>();
        //获取最近12个月
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            months.add(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1));
            calendar.add(Calendar.MONTH, +1);

        }
            ArrayList<Integer> memberCount = new ArrayList<>();
            for (String month : months) {
                int count = memberDao.getMemberCountByMonth(month+"-31");
                memberCount.add(count);
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("months",months);
            map.put("memberCount",memberCount);
        return map;
    }
    //根据性别查询套餐报告
    @Override
    public Map<String, Object> getMemberReportBySex() {
        List<Map<String, Object>>  list = memberDao.getMemberReportBySex();
        HashMap<String, Object> resultMap = new HashMap<>();
        List<String> memberSex= new ArrayList<>();
        List<Map<String, Object>> memberCount = new ArrayList<>();
        for (Map<String, Object> map : list) {
            HashMap<String, Object> memberCountMap = new HashMap<>();
            Object sex = map.get("sex");
            Object value = map.get("value");
            if ("1".equals(sex)){

                memberSex.add("男");
                memberCountMap.put("name","男");
                memberCountMap.put("value",value);
                memberCount.add(memberCountMap);
            }else if ("2".equals(sex)){
                memberSex.add("女");
                memberCountMap.put("name","女");
                memberCountMap.put("value",value);
                memberCount.add(memberCountMap);
            }else{
                memberSex.add("错误");
                memberCountMap.put("name","错误");
                memberCountMap.put("value",value);
                memberCount.add(memberCountMap);
            }
        }
        resultMap.put("memberSex",memberSex);
        resultMap.put("memberCount",memberCount);
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> getSexRepot() {
      return   memberDao.getSexRepot();
    }

    @Override
    public List<Map<String, Object>> findByMemberAge() {
        return memberDao.findByMemberAge();
    }
}
